package com.gt.ao.views;

import com.gt.ao.utils.Base64Util;
import com.gt.ao.utils.QRUtil;
import com.gt.ao.utils.TextSplitUtil;
import com.gt.ao.utils.ThreadPoolSizeUtil;

import javax.swing.*;
import java.io.File;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author gt.ao
 * @description //选中文件并转为二维码文件
 * @createTime  2022/7/11 17:01
 **/
public class ChooseFileView {

    static ExecutorService executorService = Executors.newFixedThreadPool(ThreadPoolSizeUtil.cpuIntesivePoolSize());

    public static void main(String[] args) {

        JFileChooser jfc=new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );
        jfc.setVisible(true);
        int result = jfc.showDialog(new JLabel(), "选择");
        if (result == JFileChooser.APPROVE_OPTION) {

        } else if (result == JFileChooser.CANCEL_OPTION) {
            System.out.println("Cancel was selected");
            return;
        }

        File f = jfc.getSelectedFile();
        long length = f.length();
        String name = f.getName();
        String path = f.getAbsolutePath();
        //104857600 == 100m
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("文件大小==="+df.format(new BigDecimal(length).divide(new BigDecimal(1024))) +"kb");
        System.out.println("文件名称==="+name);
        System.out.println("文件路径==="+path);
        if(length<=0){
            JOptionPane.showMessageDialog(null,"请选择非空文件！","提示",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(length>104857600){
            JOptionPane.showMessageDialog(null,"文件超过100m，请重新选择！若需转换超过100m的文件，可使用WINRAR压缩工具进行文件拆分！","提示",JOptionPane.WARNING_MESSAGE);
            return;
        }
        String buffer = System.getProperty("buffer");
        if(buffer==null){
            System.setProperty("buffer","800");
            buffer = "800";
        }
        if(!TextSplitUtil.isNumber(buffer)){
            JOptionPane.showMessageDialog(null,"参数不合法！","提示",JOptionPane.WARNING_MESSAGE);
            return;
        }
        int intbuffer = Integer.parseInt(buffer);
        if(intbuffer < 0 || intbuffer > 2048 ){
            JOptionPane.showMessageDialog(null,"参数不合法！","提示",JOptionPane.WARNING_MESSAGE);
            return;
        }
        long start = System.currentTimeMillis();
        try {
            QRCodePreview qr = new QRCodePreview();
            TreeMap<Integer,String> splitFiles = TextSplitUtil.splitFileMem(f.getAbsolutePath());
            long splitEnd = System.currentTimeMillis();
            System.out.println("拆分文件耗时："+((splitEnd-start))+"ms");
            int size = splitFiles.size();
            //List<byte[]> qrcodes = Collections.synchronizedList(new ArrayList<>(size+1));
            SortedMap<Integer,byte[]> sortedMap = Collections.synchronizedSortedMap(new TreeMap<>());
            byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
            QRUtil.encodeBytes(("yr://0|"+(size+1)+"|"+nameBytes.length+",")+Base64Util.encodeBase64Str(nameBytes),sortedMap,0);
            CountDownLatch countDownLatch = new CountDownLatch(size);
            for(int i=1;i<=size;i++){
                String b = splitFiles.get(i);
                final int ii = i;
                executorService.submit(() -> {
                    QRUtil.encodeBytes(b,sortedMap,ii);
                    countDownLatch.countDown();
                });
            }
            countDownLatch.await();
            long qrcodeEnd = System.currentTimeMillis();
            System.out.println("二维码生成耗时："+((qrcodeEnd-splitEnd)/1000)+"s");
            qr.setQrcodes(sortedMap);
            qr.setInterval(200);
            splitFiles = null;
            long end = System.currentTimeMillis();
            System.out.println("共耗时："+((end-start)/1000)+"s");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
